<?php
/**
 * Image helper
 */
class Helper_Image
{
    public $view;
    
    const UPLOAD_DIR='data';
    const THUMBS_DIR='thumbs';
    
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
    
    /**
     * Resizes image and returns its html code
     * 
     * @param string $folder (must be in UPLOAD_DIR)
     * @param string $filename (without file extension)
     * @param string $file_ext
     * @param int $width
     * @param int $height
     * @param bool $crop
     * @param array $attributes
     * @param bool $force_dst_type (should we keep image type)
     * 
     * @return string (html)
     */
    public function image($folder,$filename,$file_ext='',$width,$height,$crop=false,$attributes=array(),$force_dst_type=false)
    {
        $image_src_suffix=$folder.'/'.$filename.(!empty($file_ext) ? '.'.quotemeta($file_ext) : '');
        $image_path_suffix=self::UPLOAD_DIR.'/'.self::THUMBS_DIR.'/'.quotemeta($folder).'/'.quotemeta($filename).(!empty($file_ext) ? '.'.quotemeta($file_ext) : '');
        $image_dimm_suffix='_'.(int) $width.'x'.(int) $height;
        
        $attributes_str=' ';
        if (!empty($attributes))
        {
            foreach ($attributes as $attr_name=>$attr_val)
            {
                $attributes_str.=htmlentities($attr_name).'="'.htmlentities($attr_val).'" ';
            }
        }
        
        if (!$this->checkThumbsFolder($folder)
            || (!file_exists(ROOT_DIR.'/'.$image_path_suffix.$image_dimm_suffix)
            && !$this->resizeImage(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.$image_src_suffix,ROOT_DIR.'/'.$image_path_suffix.$image_dimm_suffix,$width,$height,$crop,$force_dst_type))
            ) {
            return '<img src="'.$this->view->baseUrl($image_src_suffix).'"'.$attributes_str.' />';
        }
        
        return '<img src="'.$this->view->baseUrl($image_path_suffix.$image_dimm_suffix).'" width="'.(int) $width.'" height="'.(int) $height.'"'.$attributes_str.' />';
    }
    
    protected function resizeImage($img_src_path,$img_dst_path,$dst_width,$dst_height,$crop=false,$force_dst_type=false)
    {
        if (!function_exists('gd_info')) return false;

        $src=false;
        $size=@getimagesize($img_src_path);
        if (!$size) return false;
        
        switch ($size[2])
        {
            case IMAGETYPE_JPEG :
                $src=@imagecreatefromjpeg($img_src_path);
                $dst_type='jpeg';
                break;
            case IMAGETYPE_PNG :
                $src=@imagecreatefrompng($img_src_path);
                $dst_type='png';
                break;
            case IMAGETYPE_GIF :
                $src=@imagecreatefromgif($img_src_path);
                $dst_type='gif';
                break;
        }
        
        if ($src)
        {
            $src_width=$size[0];
            $src_height=$size[1];
            $src_x=0;
            $src_y=0;
                        
            $dst_x=0;
            $dst_y=0;
            
            $dst=imagecreatetruecolor($dst_width,$dst_height);
            $bg_color=@imagecolorallocate($dst, 255, 255, 255);
            @imagefill($dst, 0, 0, $bg_color);
        
            if ($src_width<$dst_width && $src_height<$dst_height)
            {
                $dst_x=floor(($dst_width-$src_width)/2);
                $dst_y=floor(($dst_height-$src_height)/2);
                $dst_width=$src_width;
                $dst_height=$src_height;
            }
            elseif ($crop)
            {
                switch (true)
                {
                    case ($dst_width/$dst_height) > ($src_width/$src_height) :
                        $src_height=$src_width*($dst_height/$dst_width);
                        $src_y=($size[1]-$src_height)/2;
                        break;
                    case ($dst_width/$dst_height) < ($src_width/$src_height) :
                        $src_width=$src_height*($dst_width/$dst_height);
                        $src_x=($size[0]-$src_width)/2;
                        break;
                }
            } else 
            {
                switch (true)
                {
                    case ($dst_width/$dst_height) < ($src_width/$src_height) :
                        $_dst_height=$dst_width*($src_height/$src_width);
                        $dst_y=($dst_height-$_dst_height)/2;
                        $dst_height=$_dst_height;
                        unset($_dst_height);
                        break;
                    case ($dst_width/$dst_height) > ($src_width/$src_height) :
                        $_dst_width=$dst_height*($src_width/$src_height);
                        $dst_x=($dst_width-$_dst_width)/2;
                        $dst_width=$_dst_width;
                        unset($_dst_width);
                        break;
                }
            }
            
            if (!@imagecopyresampled($dst,$src,$dst_x,$dst_y,$src_x,$src_y,$dst_width,$dst_height,$src_width,$src_height)) return false;
            
            if (!empty($force_dst_type)) $dst_type=$force_dst_type;
            
            switch ($dst_type)
            {
                case 'jpeg' :
                    if (!@imagejpeg($dst,$img_dst_path)) return false;
                    break;
                case 'png' :
                    if (!@imagepng($dst,$img_dst_path)) return false;
                    break;
                case 'gif' :
                    if (!function_exists('imagegif') || !@imagegif($dst,$img_dst_path)) return false;
                     break;
                 default:
                     if (!@imagejpeg($dst,$img_dst_path)) return false;
                     break;
            }
                    
            imagedestroy($src);
            imagedestroy($dst);
            
            return true;
        } else return false;
    }
    
    protected function checkThumbsFolder($folder)
    {
        if (!file_exists(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR)) return false;
        if (!is_dir(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR)) return false;
        if (!is_writable(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR)) return false;
        
        if (!file_exists(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR.'/'.quotemeta($folder)))
        {
            @mkdir(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR.'/'.quotemeta($folder),0777);
        }
        
        if (!file_exists(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR.'/'.quotemeta($folder))) return false;
        if (!is_dir(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR.'/'.quotemeta($folder))) return false;
        if (!is_writable(ROOT_DIR.'/'.self::UPLOAD_DIR.'/'.self::THUMBS_DIR.'/'.quotemeta($folder))) return false;
        
        return true;
    }
}
